package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.converter.SubjectCateforyDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.converter.SubjectInfoConverter;
import com.jingdianjichi.subject.eneity.SubjectCategoryBO;
import com.jingdianjichi.subject.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 添加分类
     * @param subjectCategoryDTO 添加的信息类
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            //这个方法的作用是检查当前日志记录级别是否允许记录信息级别的日志消息。如果允许，则返回true；否则返回false。
            //假设日志记录级别设置为“info”，那么调用log.isInfoEnabled()将返回true，因为信息级别的日志消息是允许的。
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.add.dto:{} ", JSON.toJSONString(subjectCategoryDTO));
            }
            System.out.println(subjectCategoryDTO);
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"父级id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCateforyDTOConverter.INSTANCE.converterDtoToCategoryBo(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.add.error:{} ",e.getMessage(),e);
            return Result.fail("新增分类失败");
        }
    }


    /**
     * 查询分类模块 分为有条件查询和无条件查询 条件就是前端传过来的数据
     * 所以我统一接口 当前端传值时候我就用他的值查询 不传值我就创建个空对象来保证接口可以统一使用
     * @return
     */
    /**
     * 查询一级分类
     * @return
     */
    @RequestMapping("/queryPrimaryCategory")

    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            // 空对象
            SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCateforyDTOConverter.INSTANCE.converterBosToDtos(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOS);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{} ",e.getMessage(),e);
            return Result.fail("查询失败");

        }
    }

    /**
     * 查询二级分类
     * @return
     */
    @RequestMapping("/queryCategoryByPrimary")

    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{} ", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCateforyDTOConverter.INSTANCE.converterDtoToCategoryBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCateforyDTOConverter.INSTANCE.converterBosToDtos(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOS);

        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{} ",e.getMessage(),e);
            return Result.fail("查询失败");

        }
    }

    /**
     * 更新分类
     * @return
     */
    @RequestMapping("/update")

    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.update.dto:{} ", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCateforyDTOConverter.INSTANCE.converterDtoToCategoryBo(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.update.error:{} ",e.getMessage(),e);
            return Result.fail("更新分类失败");

        }
    }

    /**
     * 删除分类
     * @return
     */
    @RequestMapping("/delete")

    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.delete.dto:{} ", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCateforyDTOConverter.INSTANCE.converterDtoToCategoryBo(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.delete.error:{} ",e.getMessage(),e);
            return Result.fail("更新分类失败");

        }
    }


}
